﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class SearchData
    {
        public void Add(KeRui.Model.SearchData model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Tag", SqlDbType.NVarChar, 20), new SqlParameter("@DataID", SqlDbType.Int, 4), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@Title", SqlDbType.NVarChar, 200), new SqlParameter("@UpdateTime", SqlDbType.DateTime), new SqlParameter("@CreateTime", SqlDbType.DateTime) };
            parameters[0].Value = model.Tag;
            parameters[1].Value = model.DataID;
            parameters[2].Value = model.State;
            parameters[3].Value = model.Title;
            parameters[4].Value = model.UpdateTime;
            parameters[5].Value = model.CreateTime;
            DbHelperSQL.RunProcedure("SearchData_ADD", parameters, out num);
        }

        public void Delete(string Tag, int DataID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Tag", SqlDbType.NVarChar, 50), new SqlParameter("@DataID", SqlDbType.Int, 4) };
            parameters[0].Value = Tag;
            parameters[1].Value = DataID;
            DbHelperSQL.RunProcedure("SearchData_Delete", parameters, out num);
        }

        public KeRui.Model.SearchData GetModel(string Tag, int DataID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Tag", SqlDbType.NVarChar, 50), new SqlParameter("@DataID", SqlDbType.Int, 4) };
            parameters[0].Value = Tag;
            parameters[1].Value = DataID;
            KeRui.Model.SearchData data = new KeRui.Model.SearchData();
            DataSet set = DbHelperSQL.RunProcedure("SearchData_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                data.Tag = set.Tables[0].Rows[0]["Tag"].ToString();
                if (set.Tables[0].Rows[0]["DataID"].ToString() != "")
                {
                    data.DataID = int.Parse(set.Tables[0].Rows[0]["DataID"].ToString());
                }
                if (set.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    data.State = int.Parse(set.Tables[0].Rows[0]["State"].ToString());
                }
                data.Title = set.Tables[0].Rows[0]["Title"].ToString();
                if (set.Tables[0].Rows[0]["UpdateTime"].ToString() != "")
                {
                    data.UpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["UpdateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    data.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                return data;
            }
            return null;
        }

        public void Update(KeRui.Model.SearchData model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Tag", SqlDbType.NVarChar, 20), new SqlParameter("@DataID", SqlDbType.Int, 4), new SqlParameter("@State", SqlDbType.Int, 4), new SqlParameter("@Title", SqlDbType.NVarChar, 200), new SqlParameter("@UpdateTime", SqlDbType.DateTime), new SqlParameter("@CreateTime", SqlDbType.DateTime) };
            parameters[0].Value = model.Tag;
            parameters[1].Value = model.DataID;
            parameters[2].Value = model.State;
            parameters[3].Value = model.Title;
            parameters[4].Value = model.UpdateTime;
            parameters[5].Value = model.CreateTime;
            DbHelperSQL.RunProcedure("SearchData_Update", parameters, out num);
        }
    }
}

